package com.lsz.intelligentflowerwatering.ui

import android.bluetooth.BluetoothDevice
import android.content.Intent
import android.location.LocationManager
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Bluetooth
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.lsz.intelligentflowerwatering.ui.theme.MyComposeTheme
import com.lsz.intelligentflowerwatering.ui.theme.listBackground
import com.lsz.intelligentflowerwatering.viewmodel.BleViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import com.lsz.intelligentflowerwatering.bean.BleDevice
import com.lsz.intelligentflowerwatering.viewmodel.BleState
import com.lsz.intelligentflowerwatering.viewmodel.MainViewModel
import com.lsz.intelligentflowerwatering.viewmodel.WifiViewModel

@Composable
fun OnLoadingProgress(
    state: MutableState<Boolean>
) {
    if (state.value) {
        Dialog(onDismissRequest = { }) {
            CircularProgressIndicator()
        }
    }
}


@OptIn(ExperimentalAnimationApi::class, ExperimentalMaterialApi::class)
@Composable
fun BleConfig(
    bleViewModel: BleViewModel = viewModel(),
    mainViewModel: MainViewModel = viewModel(),
    wifiViewModel: WifiViewModel = viewModel(),
    connect:(BleDevice)->Unit = {}
) {
    val navController: NavHostController = rememberAnimatedNavController()
    val loadingState = remember { mutableStateOf(false) }

    if( bleViewModel.bleState == BleState.DisConnect ){
        toast(LocalContext.current,"连接失败")
    }
    else if( bleViewModel.bleState == BleState.ConnectBegin ){
        loadingState.value = true
    }
    else if( bleViewModel.bleState == BleState.ConnectEnd ){
        loadingState.value = false
        navController.navigate("wifiConfig")
    }
    Column(
        Modifier
            .padding(30.dp)
            .background(color = MyComposeTheme.colors.background)
    ) {
        TheText(text = "可选设备")
        LazyColumn(
            modifier = Modifier
                .fillMaxWidth()
                .weight(4f)
                .background(listBackground, shape = RoundedCornerShape(20.dp))
                .padding(30.dp)
        ) {
            itemsIndexed(bleViewModel.bleDeviceList) { index, item ->
                Row(
                    Modifier
                        .fillMaxWidth()
                        .height(60.dp)
                        .clickable {
                            connect(item)
                        },
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Icon(
                        Icons.Filled.Bluetooth,
                        "蓝牙",
                        tint = MyComposeTheme.colors.listContent,
                        modifier = Modifier
                            .size(30.dp)
                            .padding(start = 10.dp)
                    )
                    Column(
                        Modifier
                            .padding(start = 10.dp)
                            .fillMaxWidth()
                    ) {
                        Text(text = item.name, color = MyComposeTheme.colors.listContent)
                        Text(
                            text = item.bluetoothDevice.address,
                            color = MyComposeTheme.colors.listContent
                        )
                    }
                }
                if (index < bleViewModel.bleDeviceList.lastIndex) {
                    Divider()
                }
            }
        }

        Divider(thickness = 30.dp, color = MyComposeTheme.colors.background)

        Row(
            modifier = Modifier
                .weight(1f)
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.Center
        ) {
            TheButton("重新扫描", onClick = {bleViewModel.bleScanStart()})
        }
        OnLoadingProgress(loadingState)
    }
}